<?php

namespace App\Http\Controllers\V1;

use App\Http\Controllers\ApiBaseController;
use App\Http\Requests\CustomerProductRequest;
use App\Http\Requests\UserGroupProductRequest;
use App\Models\CustomerProduct;
use App\Models\CustomerProductChannel;
use App\Models\UserGroupProduct;
use App\Models\UserGroupProductChannel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class UserGroupProductController extends ApiBaseController
{
    public function index(Request $request)
    {

        $paginator = UserGroupProduct::with(['product', 'userGroup', 'spec', 'channel_group', 'channels.channel'])->filter($request->all())->paginate($this->pageSize);
        return success(page_format($paginator));
    }

    public function show(Request $request, UserGroupProduct $model)
    {
        $id = $request->get('id');
        $data = $model->with(['product', 'spec', 'channels.channel'])->find($id);
        return success(['data' => $data->toArray()]);
    }

    public function addProduct(UserGroupProductRequest $request)
    {
        $params = $request->all();

        DB::transaction(function () use ($params) {
            $items = $params['items'];
            $channels = $params['channels'];
            if (isset($params['submit_order_count']) && $params['submit_order_count']) {
                $submit_order_count = $params['submit_order_count'];
            } else {
                $submit_order_count = env('ORDER_RE_SUBMIT_LIMIT');
            }
            if (count($items) > 0) {
                foreach ($items as $item) {
                    $model = UserGroupProduct::updateOrCreate(
                        [
                            'spec_id' => intval($item['spec_id']),
                            'user_group_id' => intval($item['user_group_id']),
                        ],
                        [
                            'product_id' => intval($item['product_id']),
                            'user_group_price' => floatval($item['user_group_price']),
                            'channel_group_id' => floatval($item['channel_group_id']),
                            'is_loop' =>  $item['is_loop'] ?? 0,
                            'sort_type' => $params['sort_type'] ?? 1,
                            'timeout' =>  intval($item['timeout']) ?? 0,
                            'submit_order_count' =>  intval($submit_order_count),

                        ]
                    );
                    UserGroupProductChannel::where('user_group_product_id', $model->id)->delete();
                    if (intval($params['channel_group_id']) == 0) {
                        foreach ($channels as $channel) {
                            $tmp_add = [
                                'channel_id' => intval($channel['channel_id']),
                                'submit_nums' => intval($channel['submit_nums']),
                                'sort' => intval($channel['sort']),
                                'user_group_product_id' => $model->id,
                            ];
                            UserGroupProductChannel::create($tmp_add);
                        }
                    }
                }
            }
        });
        return success([], '添加成功');
    }
    public function changeUserGroupDiscount(Request $request)
    {
        $params = $request->only(['ids', 'discount']);
        if (!isset($params['ids']) || !isset($params['discount'])) {
            return error('ids 或者discount 不能为空');
        }
        if ($params['discount'] > 100) {
            return error('折扣不能大于100');
        }
        // 
        foreach ($params['ids'] as $key => $value) {
            //查询价格
            $info = UserGroupProduct::with(['spec'])->find($value);
            //原价
            $oldPrice = $info->spec->market_price;
            $newPrice = $oldPrice * $params['discount'] / 100;
            $info->user_group_price = $newPrice;
            $info->save();
        }
        return success();
    }

    public function destroy($ids, Request $request, UserGroupProduct $model)
    {
        $id_arr = explode(',', $ids);
        DB::transaction(function () use ($id_arr, $model) {
            $model->destroy($id_arr);
            DB::table('cz_user_group_product_channel')->whereIn('user_group_product_id', $id_arr)->delete();
        });
    }
}
